#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <net/if.h>

int main(void)
{
    struct ifreq ifr = {0};
    struct sockaddr_can can_addr = {0};
    struct can_frame frame = {0};
    int sockfd = -1;
    int ret;

    sockfd = socket(PF_CAN,SOCK_RAW,CAN_RAW);
    if(0>sockfd){
        perror("socket error");
        exit(EXIT_FAILURE);
    }
    strcpy(ifr.ifr_name,"can0");
    ioctl(sockfd,SIOCGIFINDEX,&ifr);
    can_addr.can_family = AF_CAN;
    can_addr.can_ifindex = ifr.ifr_ifindex;

    ret = bind(sockfd,(struct sockaddr*)&can_addr,sizeof(can_addr));
    if(0>ret){
        perror("bind error");
        close(sockfd);
        exit(EXIT_FAILURE);
    }

    setsockopt(sockfd,CAN_RAW,CAN_RAW_FILTER,NULL,0);

    frame.data[0]=0xA0;
    frame.data[1]=0xB0;
    frame.data[2]=0xC0;
    frame.data[3]=0xD0;
    frame.data[4]=0xE0;
    frame.data[5]=0xF0;
    frame.can_dlc = 6;
    frame.can_id = 0x123;

    for(;;){
        ret = write(sockfd,&frame,sizeof(frame));
        if(sizeof(frame)!=ret){
            perror("write error");
            goto out;
        }
        sleep(1);
    }

out:
    close(sockfd);
    exit(EXIT_SUCCESS);
}




